import { MemoryManager } from "GameLoop/DecisionMaking/MemoryManager";
import { ABaseEntity } from "../BaseEntity";

export abstract  class GlobalEntity< T extends INoMemory,V extends IRoomModuleInitializer=IRoomModuleInitializer > extends ABaseEntity< T,V >
{
    public get region():string{
        return Game.shard.name
    }
    public   getNodeMemory (): T {
        const regionCache = MemoryManager.getMemory<IMemoryCache>(this.region);
        if(!regionCache.Nodes ||!regionCache.Nodes[this.key])
        {
            throw new Error(`${this.GUID}->${this.entityType()} 的内存未初始化`)
        }
        return regionCache.Nodes[this.key]
    }
    public initAttributes(): void {
        this.initNodeMemory();
    }

    public abstract updateMemory():void;
    protected initNodeMemory()
    {
        const regionCache = MemoryManager.getMemory<IMemoryCache>(this.region);
        if(!regionCache)
        {
            this.log('error', `找不到内存`,this.region);
        }
        if(!regionCache.Nodes)
        {
            regionCache.Nodes={};
        }
        if(!regionCache.Nodes[this.key])
        {
            regionCache.Nodes[this.key]={};
        }
    }
}